/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SubsistemaPaciente.Derivacion;

import clienteSandraParada.Login;
import java.awt.Color;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Hernán Garrido
 */
public class Derivacion extends javax.swing.JPanel {
                        
    public Derivacion(){
        initComponents();
        this.actualizarTabla();
        this.ocultarColumnaID();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dialogoEliminarDerivacion = new javax.swing.JDialog();
        botonSiEliminar = new javax.swing.JButton();
        botonNoEliminar = new javax.swing.JButton();
        etiquetaEliminar = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tablaDerivaciones = new javax.swing.JTable();
        datosABuscar = new javax.swing.JTextField();
        jPanel7 = new javax.swing.JPanel();
        botonAgregarDerivacion = new javax.swing.JButton();
        botonEliminarDerivacion = new javax.swing.JButton();
        botonModificarDerivacion = new javax.swing.JButton();
        botonVerDerivacion = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        etiquetaMensajeria = new javax.swing.JLabel();

        dialogoEliminarDerivacion.setTitle("Eliminar derivación");
        dialogoEliminarDerivacion.setMinimumSize(new java.awt.Dimension(420, 160));
        dialogoEliminarDerivacion.setModal(true);
        dialogoEliminarDerivacion.setResizable(false);

        botonSiEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Aceptar.png"))); // NOI18N
        botonSiEliminar.setText("Si");
        botonSiEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSiEliminarActionPerformed(evt);
            }
        });

        botonNoEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Cancelar.png"))); // NOI18N
        botonNoEliminar.setText("No");
        botonNoEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonNoEliminarActionPerformed(evt);
            }
        });

        etiquetaEliminar.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etiquetaEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos35X35/Papelera.png"))); // NOI18N
        etiquetaEliminar.setText("¿Confirma que desea enviar a la papelera la derivación seleccionada?");

        javax.swing.GroupLayout dialogoEliminarDerivacionLayout = new javax.swing.GroupLayout(dialogoEliminarDerivacion.getContentPane());
        dialogoEliminarDerivacion.getContentPane().setLayout(dialogoEliminarDerivacionLayout);
        dialogoEliminarDerivacionLayout.setHorizontalGroup(
            dialogoEliminarDerivacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, dialogoEliminarDerivacionLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(botonSiEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonNoEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addComponent(etiquetaEliminar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
        );
        dialogoEliminarDerivacionLayout.setVerticalGroup(
            dialogoEliminarDerivacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(dialogoEliminarDerivacionLayout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(etiquetaEliminar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addGroup(dialogoEliminarDerivacionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonSiEliminar)
                    .addComponent(botonNoEliminar))
                .addContainerGap())
        );

        setMaximumSize(new java.awt.Dimension(953, 603));
        setPreferredSize(new java.awt.Dimension(953, 603));

        jPanel5.setBackground(new java.awt.Color(255, 255, 255));
        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1), "Derivaciones", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 0, 36))); // NOI18N
        jPanel5.setMaximumSize(new java.awt.Dimension(950, 32767));
        jPanel5.setPreferredSize(new java.awt.Dimension(950, 451));

        jLabel4.setText("Buscar ");

        tablaDerivaciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Rut paciente", "Nombre paciente", "Rut profesional", "Nombre profesional", "Fecha"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaDerivaciones.setMaximumSize(new java.awt.Dimension(950, 800));
        tablaDerivaciones.setName(""); // NOI18N
        tablaDerivaciones.getTableHeader().setReorderingAllowed(false);
        tablaDerivaciones.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaDerivacionesMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(tablaDerivaciones);

        datosABuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                datosABuscarKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap(718, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(datosABuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(datosABuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 402, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel7.setBackground(new java.awt.Color(255, 255, 255));

        botonAgregarDerivacion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos35X35/Agregar.png"))); // NOI18N
        botonAgregarDerivacion.setText("Agregar");
        botonAgregarDerivacion.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        botonAgregarDerivacion.setMargin(new java.awt.Insets(2, 4, 2, 6));
        botonAgregarDerivacion.setMaximumSize(new java.awt.Dimension(97, 43));
        botonAgregarDerivacion.setMinimumSize(new java.awt.Dimension(97, 43));
        botonAgregarDerivacion.setPreferredSize(new java.awt.Dimension(97, 43));
        botonAgregarDerivacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAgregarDerivacionActionPerformed(evt);
            }
        });

        botonEliminarDerivacion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos35X35/delete_32x32x32.png"))); // NOI18N
        botonEliminarDerivacion.setText("Eliminar");
        botonEliminarDerivacion.setMargin(new java.awt.Insets(2, 4, 2, 6));
        botonEliminarDerivacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEliminarDerivacionActionPerformed(evt);
            }
        });

        botonModificarDerivacion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos35X35/Edit.png"))); // NOI18N
        botonModificarDerivacion.setText("Modificar");
        botonModificarDerivacion.setMargin(new java.awt.Insets(2, 4, 2, 6));
        botonModificarDerivacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonModificarDerivacionActionPerformed(evt);
            }
        });

        botonVerDerivacion.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos35X35/Previsualizar.png"))); // NOI18N
        botonVerDerivacion.setText("Ver");
        botonVerDerivacion.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        botonVerDerivacion.setMargin(new java.awt.Insets(2, 4, 2, 6));
        botonVerDerivacion.setMaximumSize(new java.awt.Dimension(97, 43));
        botonVerDerivacion.setMinimumSize(new java.awt.Dimension(97, 43));
        botonVerDerivacion.setPreferredSize(new java.awt.Dimension(97, 43));
        botonVerDerivacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonVerDerivacionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(botonAgregarDerivacion, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonModificarDerivacion, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonEliminarDerivacion, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonVerDerivacion, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jPanel7Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonAgregarDerivacion, botonEliminarDerivacion, botonModificarDerivacion, botonVerDerivacion});

        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap(20, Short.MAX_VALUE)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonEliminarDerivacion, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonAgregarDerivacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonModificarDerivacion)
                    .addComponent(botonVerDerivacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel7Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botonAgregarDerivacion, botonEliminarDerivacion, botonModificarDerivacion, botonVerDerivacion});

        jPanel1.setBackground(java.awt.Color.white);

        etiquetaMensajeria.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etiquetaMensajeria.setText("Área de mensajería");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(etiquetaMensajeria, javax.swing.GroupLayout.DEFAULT_SIZE, 953, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(etiquetaMensajeria, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 953, Short.MAX_VALUE)
            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 490, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public void actualizarTabla(){
        this.limpiarTabla();
        java.util.List<service.Derivacion> derivaciones = listarDerivacion();
        int i = 0;
        for(service.Derivacion p : derivaciones){
            if(p.isVisible()){
                this.agregarFila();
                String nombrePac = p.getNombresPaciente()+" "+p.getApellidoPaternoPaciente()+" "+p.getApellidoMaternoPaciente();
                String nombreProf = p.getNombresProfesional()+" "+p.getApellidoPaternoProfesional()+" "+p.getApellidoMaternoProfesional();
                tablaDerivaciones.setValueAt(p.getId().toString(), i, 0);
                tablaDerivaciones.setValueAt(p.getRutPaciente(), i, 1);
                tablaDerivaciones.setValueAt(nombrePac, i, 2);
                tablaDerivaciones.setValueAt(p.getRutProfesional(), i, 3);
                tablaDerivaciones.setValueAt(nombreProf, i, 4);
                tablaDerivaciones.setValueAt(p.getFecha(), i, 5);
                i++;
            }
        }
    }
    
    public void agregarFila(){
        DefaultTableModel temp = (DefaultTableModel) tablaDerivaciones.getModel();
        Object nuevo[]= {"","","","","",""};
        temp.addRow(nuevo);
    }
    
    private void botonModificarDerivacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonModificarDerivacionActionPerformed
        this.normalizarEtiquetaMensajeria();
        if(tablaDerivaciones.getSelectedRow() == -1){
            etiquetaMensajeria.setText("Debe seleccionar una derivación para ser modificada");
            etiquetaMensajeria.setForeground(Color.red);
        }
        else{
            DialogoInfoDerivacion dip = new DialogoInfoDerivacion(null, true, this, Long.parseLong((String)tablaDerivaciones.getValueAt(tablaDerivaciones.getSelectedRow(),0)));
            dip.normalizarMensajeria();
            dip.setLocationRelativeTo(null);
            dip.setTitle("Modificar Derivación");
            dip.getBotonAceptar().setText("Guardar");
            dip.getBotonAceptar().setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Aceptar.png")));
            service.Derivacion der = (service.Derivacion)cargarDerivacion((Long.parseLong((String)tablaDerivaciones.getValueAt(tablaDerivaciones.getSelectedRow(),0))));
            dip.cargarUsuarios();
            dip.cargarPacientes();
            dip.llenarCampos(der);
            dip.setVisible(true);
        }
    }//GEN-LAST:event_botonModificarDerivacionActionPerformed

    private void botonEliminarDerivacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEliminarDerivacionActionPerformed
        this.normalizarEtiquetaMensajeria();
        int cont = 0;
        for(int i=0; i<tablaDerivaciones.getRowCount();i++ ){
            if(tablaDerivaciones.isRowSelected(i)){
                cont++;
            }
        }
        if(cont >= 1){            
            if(cont>1){
                etiquetaEliminar.setText("¿Confirma que desea enviar a la papelera las derivaciones seleccionadas?");
            }
            else if(cont==1){
                etiquetaEliminar.setText("¿Confirma que desea enviar a la papelera la derivación seleccionada?");
            }
            dialogoEliminarDerivacion.setLocationRelativeTo(null);
            dialogoEliminarDerivacion.setVisible(true);
        }
        else{
            etiquetaMensajeria.setText("Debe seleccionar una derivación para ser eliminada");
            etiquetaMensajeria.setForeground(Color.red);
            
        }
    }//GEN-LAST:event_botonEliminarDerivacionActionPerformed

    private void botonAgregarDerivacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAgregarDerivacionActionPerformed
        this.normalizarEtiquetaMensajeria();
        DialogoInfoDerivacion dip = new DialogoInfoDerivacion(null, true, this, null);
        dip.normalizarMensajeria();
        dip.limpiarCampos();
        dip.setLocationRelativeTo(null);
        dip.setTitle("Agregar Derivación");
        dip.getBotonAceptar().setText("Agregar");
        dip.getBotonAceptar().setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Agregar.png")));
        dip.cargarUsuarios();
        dip.cargarPacientes();
        dip.setVisible(true);
    }//GEN-LAST:event_botonAgregarDerivacionActionPerformed
    
    private void botonNoEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonNoEliminarActionPerformed
        dialogoEliminarDerivacion.dispose();
    }//GEN-LAST:event_botonNoEliminarActionPerformed
    
    private void botonSiEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSiEliminarActionPerformed
        dialogoEliminarDerivacion.dispose();
        for(int i=0; i<tablaDerivaciones.getRowCount();i++ ){
            if(tablaDerivaciones.isRowSelected(i)){
                String valorNuevo="Rut paciente: "+(String) tablaDerivaciones.getValueAt(i, 1)+"\n"+"Rut profesional: "+(String) tablaDerivaciones.getValueAt(i, 3)+"\n"+"Fecha: "+(String) tablaDerivaciones.getValueAt(i, 5);
                eliminarDerivacion(Long.parseLong((String)tablaDerivaciones.getValueAt(i,0)));
                registrar(Login.getUser(), "Derivación", "Eliminó derivación", "No aplica", valorNuevo);
            }
        }
        this.actualizarTabla();
        
    }//GEN-LAST:event_botonSiEliminarActionPerformed

    private void botonVerDerivacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonVerDerivacionActionPerformed
        this.normalizarEtiquetaMensajeria();        
        if(tablaDerivaciones.getSelectedRow() == -1){
            etiquetaMensajeria.setText("Debe seleccionar una derivación para verla");
            etiquetaMensajeria.setForeground(Color.red);
        }
        else{
            DialogoVerDerivacion dip = new DialogoVerDerivacion(null, true, Long.parseLong((String)tablaDerivaciones.getValueAt(tablaDerivaciones.getSelectedRow(),0)));
            dip.setLocationRelativeTo(null);
            dip.setTitle("Ver Derivación");
            service.Derivacion pres = (service.Derivacion)cargarDerivacion((Long.parseLong((String)tablaDerivaciones.getValueAt(tablaDerivaciones.getSelectedRow(),0))));
            dip.llenarCampos(pres);
            dip.setVisible(true);
        }
    }//GEN-LAST:event_botonVerDerivacionActionPerformed

    private void datosABuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_datosABuscarKeyReleased
        if (!datosABuscar.getText().equals("")) {
            limpiarTabla();
            java.util.List<service.Derivacion> lista =  (java.util.List<service.Derivacion>)filtrarDerivacion(datosABuscar.getText());
            filtrarTabla(lista);
        } 
        else {
            limpiarTabla();
            actualizarTabla();
        }
    }//GEN-LAST:event_datosABuscarKeyReleased

    private void tablaDerivacionesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaDerivacionesMouseClicked
        int fila = tablaDerivaciones.rowAtPoint(evt.getPoint());
        if ((fila > -1) && evt.getClickCount() == 2) {
            botonVerDerivacionActionPerformed(null);
        }
    }//GEN-LAST:event_tablaDerivacionesMouseClicked
   
    public void cambiarEtiquetaMensajeria(String mensaje, Color color){
        etiquetaMensajeria.setText(mensaje);
        etiquetaMensajeria.setForeground(color);
    }
    
    public void filtrarTabla(java.util.List<service.Derivacion> derivaciones){
        this.limpiarTabla();
        int i = 0;
        for(service.Derivacion p : derivaciones){
            if(p.isVisible()){
                this.agregarFila();
                String nombrePac = p.getNombresPaciente()+" "+p.getApellidoPaternoPaciente()+" "+p.getApellidoMaternoPaciente();
                String nombreProf = p.getNombresProfesional()+" "+p.getApellidoPaternoProfesional()+" "+p.getApellidoMaternoProfesional();
                tablaDerivaciones.setValueAt(p.getId().toString(), i, 0);
                tablaDerivaciones.setValueAt(p.getRutPaciente(), i, 1);
                tablaDerivaciones.setValueAt(nombrePac, i, 2);
                tablaDerivaciones.setValueAt(p.getRutProfesional(), i, 3);
                tablaDerivaciones.setValueAt(nombreProf, i, 4);
                tablaDerivaciones.setValueAt(p.getFecha(), i, 5);
                i++;
            }
        }
    }
    
    public void limpiarTabla(){
        DefaultTableModel temp = (DefaultTableModel) tablaDerivaciones.getModel();
        for(int i = tablaDerivaciones.getRowCount() -1; i >= 0; i--){
            temp.removeRow(i);
        } 
    }
    
    public void normalizarEtiquetaMensajeria(){
        etiquetaMensajeria.setText("Área de mensajería");
        etiquetaMensajeria.setForeground(Color.black);
    }
    
    public void ocultarColumnaID(){
        tablaDerivaciones.getColumnModel().getColumn(0).setMaxWidth(0);
        tablaDerivaciones.getColumnModel().getColumn(0).setMinWidth(0);
        tablaDerivaciones.getColumnModel().getColumn(0).setPreferredWidth(0);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAgregarDerivacion;
    private javax.swing.JButton botonEliminarDerivacion;
    private javax.swing.JButton botonModificarDerivacion;
    private javax.swing.JButton botonNoEliminar;
    private javax.swing.JButton botonSiEliminar;
    private javax.swing.JButton botonVerDerivacion;
    private javax.swing.JTextField datosABuscar;
    private javax.swing.JDialog dialogoEliminarDerivacion;
    private javax.swing.JLabel etiquetaEliminar;
    private javax.swing.JLabel etiquetaMensajeria;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable tablaDerivaciones;
    // End of variables declaration//GEN-END:variables
                    
    private static Object cargarDerivacion(java.lang.Long id) {
        service.DerivacionWS_Service service = new service.DerivacionWS_Service();
        service.DerivacionWS port = service.getDerivacionWSPort();
        return port.cargarDerivacion(id);
    }

    private static boolean eliminarDerivacion(java.lang.Long id) {
        service.DerivacionWS_Service service = new service.DerivacionWS_Service();
        service.DerivacionWS port = service.getDerivacionWSPort();
        return port.eliminarDerivacion(id);
    } 
    
    private static java.util.List<service.Derivacion> listarDerivacion() {
        service.DerivacionWS_Service service = new service.DerivacionWS_Service();
        service.DerivacionWS port = service.getDerivacionWSPort();
        return port.listarDerivacion();
    }
    
    private static Long registrar(service.Usuario usuario, java.lang.String modulo, java.lang.String accion, java.lang.String valorAnterior, java.lang.String valorNuevo) {
        service.BitacoraWSService service = new service.BitacoraWSService();
        service.BitacoraWS port = service.getBitacoraWSPort();
        return port.registrar(usuario, modulo, accion, valorAnterior, valorNuevo);
    }

    private static java.util.List<service.Derivacion> filtrarDerivacion(java.lang.String buscar) {
        service.DerivacionWS_Service service = new service.DerivacionWS_Service();
        service.DerivacionWS port = service.getDerivacionWSPort();
        return port.filtrarDerivacion(buscar);
    }    
    
}